package aiball

import org.apache.commons.lang.builder.HashCodeBuilder

class UserAccountUserRoles implements Serializable {

	UserAccount userAccount
	UserRoles userRoles

	boolean equals(other) {
		if (!(other instanceof UserAccountUserRoles)) {
			return false
		}

		other.userAccount?.id == userAccount?.id &&
			other.userRoles?.id == userRoles?.id
	}

	int hashCode() {
		def builder = new HashCodeBuilder()
		if (userAccount) builder.append(userAccount.id)
		if (userRoles) builder.append(userRoles.id)
		builder.toHashCode()
	}

	static UserAccountUserRoles get(long userAccountId, long userRolesId) {
		find 'from UserAccountUserRoles where userAccount.id=:userAccountId and userRoles.id=:userRolesId',
			[userAccountId: userAccountId, userRolesId: userRolesId]
	}

	static UserAccountUserRoles create(UserAccount userAccount, UserRoles userRoles, boolean flush = false) {
		new UserAccountUserRoles(userAccount: userAccount, userRoles: userRoles).save(flush: flush, insert: true)
	}

	static boolean remove(UserAccount userAccount, UserRoles userRoles, boolean flush = false) {
		UserAccountUserRoles instance = UserAccountUserRoles.findByUserAccountAndUserRoles(userAccount, userRoles)
		if (!instance) {
			return false
		}

		instance.delete(flush: flush)
		true
	}

	static void removeAll(UserAccount userAccount) {
		executeUpdate 'DELETE FROM UserAccountUserRoles WHERE userAccount=:userAccount', [userAccount: userAccount]
	}

	static void removeAll(UserRoles userRoles) {
		executeUpdate 'DELETE FROM UserAccountUserRoles WHERE userRoles=:userRoles', [userRoles: userRoles]
	}

	static mapping = {
		id composite: ['userRoles', 'userAccount']
		version false
	}
}
